#pragma once 
#include <unordered_map>
#include <string>
#include <fstream>
#include <iostream>
class Dictionary
{
public:
    static std::unordered_map<std::string,std::string> _dict;
    static Dictionary*_instance;
    static Dictionary* Getinstace()
    {   
        if (_instance==nullptr)
        {
            _instance = new Dictionary();
            return _instance;
        }
        return _instance;
    }
    ~Dictionary()
    {
        if(_instance)
        {
            delete _instance;
            _instance = nullptr;
        }
    }
    std::string Translate(std::string& word)
    {
        if (_dict.find(word) == _dict.end()) return "NONE";
        return _dict[word];
    }
    void Debug()
    {
        for (auto& x : _dict)
        {
            std::cout << x.first << " : " << x.second << std::endl;
        }
    }
private:
    Dictionary() 
    {
        // std::cout << "开始加载字典" << std::endl;
        LoadDictionary();
    }
    Dictionary(const Dictionary& dic) = delete;
    Dictionary& operator =(const Dictionary& dic) = delete;

    void LoadDictionary()
    {
        std::ifstream input("Dict.txt");
        if (!input.is_open()) 
        {
            std::cout << "无法打开文件" << std::endl;
            return;
        }
        std::string line;
        while(std::getline(input,line))
        {
            //apple: 苹果 - 数据格式
            size_t pos = line.find(':');
            std::string fruit = line.substr(0,pos);
            std::string content = line.substr(pos+2);
            _dict[fruit] = content;
        }
    }

};

Dictionary* Dictionary::_instance = nullptr;
std::unordered_map<std::string,std::string> Dictionary::_dict;

